package org.zhq.core;

import java.io.OutputStream;

/**
 * @author zhengquan
 * @date 2019/2/1
 */
public abstract class ReceivePacket<Stream extends OutputStream, Entity> extends Packet<Stream> {

    //定义当前接收包的最终实体
    private Entity entity;

    public ReceivePacket(long len) {
        this.length = len;
    }

    /**
     * 得到最终接收的数据实体
     *
     * @return
     */
    public Entity entity() {
        return entity;
    }

    /**
     * 根据接收到的流转化为对应的实体
     *
     * @param stream
     * @return
     */
    protected abstract Entity buildEntity(Stream stream);

    @Override
    protected final void closeStream(Stream steam) {
        super.closeStream(steam);
        entity = buildEntity(steam);
    }


}
